package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private StatementServiceImpl statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {

        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment oldObj = this.get(appointment.getId());
        //如果不是预约中状态,不能编辑
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //合理性校验
            //如果不是预约中状态,不能删除
            Appointment oldObj = this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
                throw new BusinessException("只有状态为预约中的预约单才能删除");
            }
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void changStatus(Long id, Integer status) {
        Appointment oldObj = this.get(id);
        //如果不是预约中状态,不能取消
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能取消");
        }
        appointmentMapper.changStatus(id, Appointment.STATUS_CANCEL);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        appointmentMapper.updateStatus(id, Appointment.STATUS_ARRIVAL,new Date());

    }

    @Override
    public void updateById(Long id) {
        //合理性校验
        Appointment oldObj = this.get(id);
        //如果不是预约中状态,不能删除
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能删除");
        }
        appointmentMapper.updateById(id,Appointment.DELETE_YES);
    }

    @Override
    @Transactional
    public Long generateStatement(Long appointmentId) {
        //判断状态
        Appointment oldObj = this.get(appointmentId);
        if ( !(Appointment.STATUS_ARRIVAL.equals(oldObj.getStatus())
                || Appointment.STATUS_SETTLE.equals(oldObj.getStatus())) ) {
            throw new BusinessException("只有处于已结算或已到店才能进行结算单处理");
        }

        //通过预约单id 获取到 statement 对象
        Statement statement = statementService.getByAppointmentId(appointmentId);

        if (statement==null) {
            //说明没有结算单与它关联,生成结算单
            Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
            statement=new Statement();
            statement.setCreateTime(new Date()); //创建时间
            statement.setActualArrivalTime(appointment.getActualArrivalTime()); //实际到店时间
            statement.setCarSeries(appointment.getCarSeries()); //设置汽车类型
            statement.setCustomerName(appointment.getCustomerName()); //设置客户名称
            statement.setLicensePlate(appointment.getLicensePlate()); //设置车牌号码
            statement.setAppointmentId(appointment.getId());    //设置预约单id
            statement.setServiceType(appointment.getServiceType()); //设置服务类型
            statement.setCustomerPhone(appointment.getCustomerPhone()); //设置客户联系方式
            //保存到数据库中
            statementService.save(statement);
            //修改预约单状态为已结算
            appointmentMapper.changStatus(appointmentId,Appointment.STATUS_SETTLE);

        }
        //返回结算单id
        return statement.getId();
    }

    @Override
    public void changeStatus(Long appointmentId, Integer status) {
        appointmentMapper.changStatus(appointmentId,status);
    }
}

